{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Most Visited Sector in  a Circular Track"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #array #simulation"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数组 #模拟"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: mostVisited"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #圆形赛道上经过次数最多的扇区"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个整数 <code>n</code> 和一个整数数组 <code>rounds</code> 。有一条圆形赛道由 <code>n</code> 个扇区组成，扇区编号从 <code>1</code> 到 <code>n</code> 。现将在这条赛道上举办一场马拉松比赛，该马拉松全程由 <code>m</code> 个阶段组成。其中，第 <code>i</code> 个阶段将会从扇区 <code>rounds[i - 1]</code> 开始，到扇区 <code>rounds[i]</code> 结束。举例来说，第 <code>1</code> 阶段从&nbsp;<code>rounds[0]</code>&nbsp;开始，到&nbsp;<code>rounds[1]</code>&nbsp;结束。</p>\n",
    "\n",
    "<p>请你以数组形式返回经过次数最多的那几个扇区，按扇区编号 <strong>升序</strong> 排列。</p>\n",
    "\n",
    "<p>注意，赛道按扇区编号升序逆时针形成一个圆（请参见第一个示例）。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<p><img alt=\"\" src=\"https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2020/08/22/3rd45e.jpg\" style=\"height: 341px; width: 433px;\"></p>\n",
    "\n",
    "<pre><strong>输入：</strong>n = 4, rounds = [1,3,1,2]\n",
    "<strong>输出：</strong>[1,2]\n",
    "<strong>解释：</strong>本场马拉松比赛从扇区 1 开始。经过各个扇区的次序如下所示：\n",
    "1 --&gt; 2 --&gt; 3（阶段 1 结束）--&gt; 4 --&gt; 1（阶段 2 结束）--&gt; 2（阶段 3 结束，即本场马拉松结束）\n",
    "其中，扇区 1 和 2 都经过了两次，它们是经过次数最多的两个扇区。扇区 3 和 4 都只经过了一次。</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>n = 2, rounds = [2,1,2,1,2,1,2,1,2]\n",
    "<strong>输出：</strong>[2]\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 3：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>n = 7, rounds = [1,3,5,7]\n",
    "<strong>输出：</strong>[1,2,3,4,5,6,7]\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>2 &lt;= n &lt;= 100</code></li>\n",
    "\t<li><code>1 &lt;= m &lt;= 100</code></li>\n",
    "\t<li><code>rounds.length == m + 1</code></li>\n",
    "\t<li><code>1 &lt;= rounds[i] &lt;= n</code></li>\n",
    "\t<li><code>rounds[i] != rounds[i + 1]</code> ，其中 <code>0 &lt;= i &lt; m</code></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [most-visited-sector-in-a-circular-track](https://leetcode.cn/problems/most-visited-sector-in-a-circular-track/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [most-visited-sector-in-a-circular-track](https://leetcode.cn/problems/most-visited-sector-in-a-circular-track/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['4\\n[1,3,1,2]', '2\\n[2,1,2,1,2,1,2,1,2]', '7\\n[1,3,5,7]']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostVisited(self, n: int, rounds: List[int]) -> List[int]:\n",
    "        s = rounds[0]\n",
    "        e = rounds[-1]\n",
    "        if s <= e:\n",
    "            return list(range(s, e + 1))\n",
    "           \n",
    "        else:\n",
    "            return list(range(1, e + 1)) + list(range(s, n + 1))\n",
    "          "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostVisited(self, n: int, rounds: List[int]) -> List[int]:\n",
    "        lst = [0 for _ in range(n + 1)]\n",
    "        for i in range(len(rounds) - 1):\n",
    "            j = rounds[i] % n\n",
    "            while j != rounds[i + 1] % n:\n",
    "                lst[j] += 1\n",
    "                j = (j + 1) % n\n",
    "        lst[rounds[-1] % n] += 1\n",
    "        lst[-1] = lst[0]\n",
    "        return [i for i in range(1, len(lst)) if lst[i] == max(lst)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostVisited(self, n: int, rounds: List[int]) -> List[int]:\n",
    "        # 一直沿着一个方向跑，所有扇区的经过次数都是相同的，所以答案仅与起点和终点有关。\n",
    "        \n",
    "        start, end = rounds[0], rounds[-1]\n",
    "        if start <= end:\n",
    "            return list(range(start, end+1))\n",
    "        else:\n",
    "            return list(range(1, end+1)) + list(range(start, n+1))\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostVisited(self, n: int, rounds: List[int]) -> List[int]:\n",
    "        return [x for x in range(rounds[0], rounds[-1]+1)] if rounds[-1]>= rounds[0] else [x for x in range(1,rounds[-1]+1)] + [x for x in range(rounds[0], n+1)] "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostVisited(self, n: int, rounds: List[int]) -> List[int]:\n",
    "        # 中间无论走了几圈都不会影响结果，因此答案只和起点以及终点相关\n",
    "        start = rounds[0]\n",
    "        end = rounds[-1]\n",
    "        if start <= end:\n",
    "            return list(range(start, end + 1))\n",
    "        \n",
    "        # 如果终点小于起点，我们将最经常经过的区域拆分为两个\n",
    "        left_part = list(range(1, end + 1))\n",
    "        right_part = list(range(start, n + 1))\n",
    "        return left_part + right_part\n",
    "        \n",
    "            \n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostVisited(self, n: int, rounds: List[int]) -> List[int]:\n",
    "        return list(range(rounds[0],rounds[-1]+1)) if rounds[0]<=rounds[-1] else list(range(1,rounds[-1]+1))+list(range(rounds[0],n+1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostVisited(self, n: int, rounds: List[int]) -> List[int]:\n",
    "        \n",
    "        m = len(rounds)\n",
    "        cnt = [0] * (n + 1)\n",
    "        for i in range(1, m):\n",
    "            st = rounds[i-1]\n",
    "            while st != rounds[i]:\n",
    "                cnt[st] += 1\n",
    "                if st == n:\n",
    "                    st = 1\n",
    "                else:\n",
    "                    st += 1\n",
    "        cnt[rounds[-1]] += 1\n",
    "        mx = max(cnt)\n",
    "        ans = []\n",
    "        for i in range(1, n + 1):\n",
    "            if cnt[i] == mx:\n",
    "                ans.append(i)\n",
    "        return ans\n",
    "        \n",
    "        \n",
    "        \n",
    "        \n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostVisited(self, n: int, rounds: List[int]) -> List[int]:\n",
    "        start,end = rounds[0],rounds[-1]\n",
    "        if start<=end:return list(range(start,end+1))\n",
    "        else:\n",
    "            return list(range(1,end+1))+list(range(start,n+1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostVisited(self, n: int, rounds: List[int]) -> List[int]:\n",
    "        return list(range(rounds[0], rounds[-1]+1)) or list(range(1,rounds[-1]+1)) + list(range(rounds[0], n+1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostVisited(self, n: int, rounds: List[int]) -> List[int]:\n",
    "        start, end = rounds[0], rounds[-1]\n",
    "        if start <= end:\n",
    "            return list(range(start, end + 1))\n",
    "        else:\n",
    "            leftPart = range(1, end + 1)\n",
    "            rightPart = range(start, n + 1)\n",
    "            return list(leftPart)+list(rightPart)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostVisited(self, n: int, rounds: List[int]) -> List[int]:\n",
    "        start = rounds[0]\n",
    "        end = rounds[-1] + 1\n",
    "        if end > start:\n",
    "            return [*range(start, end)]\n",
    "        return [*chain(range(1, end), range(start, n + 1))]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostVisited(self, n: int, rounds: List[int]) -> List[int]:\n",
    "        ####i后面出现比它小的数，说明完成了一圈，只需要关注开始和结束位置即可\n",
    "        res = []\n",
    "        start, end = rounds[0], rounds[-1]\n",
    "        ####从开始到结束的扇区\n",
    "        if start > end:\n",
    "            end += n\n",
    "        for i in range(start, end + 1):\n",
    "            if i == n:\n",
    "                res.append(n)\n",
    "            else:\n",
    "                res.append(i % n)\n",
    "        res.sort()\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostVisited(self, n: int, rounds: List[int]) -> List[int]:\n",
    "        s, e = rounds[0], rounds[-1]\n",
    "        if s <= e:\n",
    "            # [起点, 终点]\n",
    "            return list(range(s, e + 1))\n",
    "        else:\n",
    "            # [1, 终点]+[起点, n]\n",
    "            return list(range(1, e + 1)) + list(range(s, n + 1))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostVisited(self, n: int, rounds: List[int]) -> List[int]:\n",
    "        a,b=rounds[0],rounds[-1]\n",
    "        list_=[]\n",
    "        if a<=b:\n",
    "            while a<=b:\n",
    "                list_.append(a)\n",
    "                a+=1\n",
    "        else:\n",
    "            while a<=n:\n",
    "                list_.append(a)\n",
    "                a+=1\n",
    "            a=1\n",
    "            while a<=b:\n",
    "                list_.append(a)\n",
    "                a+=1\n",
    "        list_.sort()\n",
    "        return list_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostVisited(self, n: int, rounds: List[int]) -> List[int]:\n",
    "        sai_dao = []\n",
    "        for i in range(rounds[0]+1,n+1):\n",
    "            sai_dao.append(i)\n",
    "        for i in range(1, rounds[0]+1):\n",
    "            sai_dao.append(i)\n",
    "        tong_ji = {}\n",
    "        tong_ji[rounds[0]] = 1\n",
    "        for i in range(1,len(rounds)):\n",
    "            while True:\n",
    "                if rounds[i]==sai_dao[0]:\n",
    "                    sai_dao.append(sai_dao[0])\n",
    "                    if sai_dao[0] in tong_ji:\n",
    "                        tong_ji[sai_dao[0]] += 1\n",
    "                    else:\n",
    "                        tong_ji[sai_dao[0]] = 1 \n",
    "                    del sai_dao[0]\n",
    "                    break\n",
    "                sai_dao.append(sai_dao[0])\n",
    "                if sai_dao[0] in tong_ji:\n",
    "                    tong_ji[sai_dao[0]] += 1\n",
    "                else:\n",
    "                    tong_ji[sai_dao[0]] = 1 \n",
    "                del sai_dao[0]\n",
    "        result = []\n",
    "        max_ = 0\n",
    "        for key,val in tong_ji.items():\n",
    "            if val>max_:\n",
    "                max_ = val\n",
    "                result = []\n",
    "                result.append(key)\n",
    "\n",
    "            elif val==max_:\n",
    "                result.append(key)\n",
    "\n",
    "\n",
    "        return sorted(result)\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostVisited(self, n: int, rounds: List[int]) -> List[int]:\n",
    "        start = rounds[0]\n",
    "        end = rounds[-1] + 1\n",
    "        if end > start:\n",
    "            return [*range(start, end)]\n",
    "        return [*chain(range(1, end), range(start, n + 1))]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostVisited(self, n: int, rounds: List[int]) -> List[int]:\n",
    "        first = rounds[0]\n",
    "        if rounds[-1] >= first:\n",
    "            return [i for i in range(first, rounds[-1]+1)]\n",
    "        else:\n",
    "            return [i for i in range(1, rounds[-1]+1)] + [i for i in range(first, n+1)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostVisited(self, n: int, rounds: List[int]) -> List[int]:\n",
    "        start = rounds[0]\n",
    "        end = rounds[-1] + 1\n",
    "        if end > start:\n",
    "            return [*range(start, end)]\n",
    "        return [*chain(range(1, end), range(start, n + 1))]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostVisited(self, n: int, rounds: List[int]) -> List[int]:\n",
    "        cnt = Counter()\n",
    "        for x, y in pairwise(rounds):\n",
    "            if y > x:\n",
    "                for i in range(x + 1, y + 1):\n",
    "                    cnt[i] += 1\n",
    "            else:\n",
    "                for i in range(x + 1, n + 1):\n",
    "                    cnt[i] += 1\n",
    "                for i in range(1, y + 1):\n",
    "                    cnt[i] += 1\n",
    "        cnt[rounds[0]] += 1\n",
    "        mx = max(cnt.values())\n",
    "        ans = []\n",
    "        for k, v in cnt.items():\n",
    "            if v == mx:\n",
    "                ans.append(k)\n",
    "        return sorted(ans)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from typing import List\n",
    "class Solution:\n",
    "    def mostVisited(self, n: int, rounds: List[int]) -> List[int]:\n",
    "        s, e = rounds[0], rounds[-1]\n",
    "        if e >= s:\n",
    "            return list(range(s, e+1))\n",
    "        else:\n",
    "            return list(range(1, e+1))+list(range(s, n+1))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostVisited(self, n: int, rounds: List[int]) -> List[int]:\n",
    "        if rounds[0] <= rounds[-1]:\n",
    "            return [i for i in range(rounds[0], rounds[-1]+1)]\n",
    "        else:\n",
    "            return [i for i in range(1, rounds[-1]+1)] + [i for i in range(rounds[0], n+1)]\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostVisited(self, n: int, rounds: List[int]) -> List[int]:\n",
    "        t = [0]*(n+1)\n",
    "        tmp = rounds[0]\n",
    "        for i in rounds[1:]:\n",
    "            if tmp < i:\n",
    "                for j in range(tmp,i):\n",
    "                    t[j] += 1\n",
    "            else:\n",
    "                for j in range(tmp, n+1):\n",
    "                    t[j] += 1\n",
    "                for k in range(1, i):\n",
    "                    t[k] += 1\n",
    "            tmp = i\n",
    "        t[i] += 1\n",
    "        res,p = [],max(t)\n",
    "        for q in range(len(t)):\n",
    "            if t[q] == p:\n",
    "                res.append(q)\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostVisited(self, n: int, rounds: List[int]) -> List[int]:\n",
    "        start = rounds[0]\n",
    "        end = rounds[-1]\n",
    "        if start <= end:\n",
    "            return list(range(start, end + 1))\n",
    "        else:\n",
    "            leftpart = range(1, end + 1)\n",
    "            rightpart = range(start, n + 1)\n",
    "            return list(itertools.chain(leftpart, rightpart))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from collections import Counter\n",
    "\n",
    "class Solution:\n",
    "    def mostVisited(self, n: int, rounds: List[int]) -> List[int]:\n",
    "        def rcount(n,rounds):\n",
    "            r = rounds[0]\n",
    "            ans = [r]\n",
    "            for i in range(1,len(rounds)):\n",
    "                while r != rounds[i]:\n",
    "                    r += 1\n",
    "                    if r > n:\n",
    "                        r = 1\n",
    "                    ans.append(r)\n",
    "            return ans\n",
    "                \n",
    "        ds = Counter(rcount(n,rounds))\n",
    "        ans,save = [],0\n",
    "        for k,v in ds.items():\n",
    "            if v > save:\n",
    "                ans = [k]\n",
    "                save = v\n",
    "            elif v == save:\n",
    "                ans.append(k)\n",
    "        return sorted(ans)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostVisited(self, n: int, rounds: List[int]) -> List[int]:\n",
    "        start, end = rounds[0], rounds[-1]\n",
    "        if start <= end:\n",
    "            return list(range(start, end+1))\n",
    "        else:\n",
    "            return list(range(1,end+1)) + list(range(start,n+1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostVisited(self, n: int, rounds: List[int]) -> List[int]:\n",
    "        start, end = rounds[0], rounds[-1]\n",
    "        if end >= start:\n",
    "            return [i for i in range(start, end + 1)]\n",
    "        else:\n",
    "            return [i for i in range(1, end + 1)] + [i for i in range(start, n + 1)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostVisited(self, n: int, rounds: List[int]) -> List[int]:\n",
    "        step = 0\n",
    "        start = rounds[0]\n",
    "        for i in range(1,len(rounds)):\n",
    "            if rounds[i-1] < rounds[i]:\n",
    "                step += (rounds[i]-rounds[i-1])\n",
    "            elif rounds[i-1] > rounds[i]:\n",
    "                step += (rounds[i]+ n -rounds[i-1])\n",
    "\n",
    "        rem = step % n # if rem == 0 return to start\n",
    "\n",
    "        res = list(range(start,start+rem+1))\n",
    "        for j in range(len(res)):\n",
    "            if res[j] > n:\n",
    "                res[j] -= n\n",
    "        \n",
    "        res.sort()\n",
    "\n",
    "        return res\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostVisited(self, n: int, rounds: List[int]) -> List[int]:\n",
    "        s, e = rounds[0], rounds[-1]\n",
    "        if s <= e:\n",
    "            # [起点, 终点]\n",
    "            return list(range(s, e + 1))\n",
    "        else:\n",
    "            # [1, 终点]+[起点, n]\n",
    "            return list(range(1, e + 1)) + list(range(s, n + 1))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# class Solution:\n",
    "#     def mostVisited(self, n: int, rounds: List[int]) -> List[int]:\n",
    "#         sai_dao = []\n",
    "#         for i in range(rounds[0]+1,n+1):\n",
    "#             sai_dao.append(i)\n",
    "#         for i in range(1, rounds[0]+1):\n",
    "#             sai_dao.append(i)\n",
    "#         tong_ji = {}\n",
    "#         tong_ji[rounds[0]] = 1\n",
    "#         for i in range(1,len(rounds)):\n",
    "#             while True:\n",
    "#                 if rounds[i]==sai_dao[0]:\n",
    "#                     sai_dao.append(sai_dao[0])\n",
    "#                     if sai_dao[0] in tong_ji:\n",
    "#                         tong_ji[sai_dao[0]] += 1\n",
    "#                     else:\n",
    "#                         tong_ji[sai_dao[0]] = 1 \n",
    "#                     del sai_dao[0]\n",
    "#                     break\n",
    "#                 sai_dao.append(sai_dao[0])\n",
    "#                 if sai_dao[0] in tong_ji:\n",
    "#                     tong_ji[sai_dao[0]] += 1\n",
    "#                 else:\n",
    "#                     tong_ji[sai_dao[0]] = 1 \n",
    "#                 del sai_dao[0]\n",
    "#         result = []\n",
    "#         max_ = 0\n",
    "#         for key,val in tong_ji.items():\n",
    "#             if val>max_:\n",
    "#                 max_ = val\n",
    "#                 result = []\n",
    "#                 result.append(key)\n",
    "#             elif val==max_:\n",
    "#                 result.append(key)\n",
    "#         return sorted(result)\n",
    "\n",
    "class Solution:\n",
    "    def mostVisited(self, n: int, rounds: List[int]) -> List[int]:\n",
    "        start, end = rounds[0], rounds[-1]\n",
    "        \n",
    "        if start <= end:\n",
    "            return list(range(start, end + 1))\n",
    "        else:\n",
    "            return list(range(1, end + 1)) + list(range(start, n + 1))\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostVisited(self, n: int, rounds: List[int]) -> List[int]:\n",
    "        l=len(rounds)\n",
    "        start=rounds[0]\n",
    "        dic={start:1}\n",
    "        maxs=[start]\n",
    "        maxn=1\n",
    "        for i in rounds[1:]:\n",
    "            if i>start:\n",
    "                for j in range(start+1,i+1):\n",
    "                    dic[j]=dic.get(j,0)+1\n",
    "                    if dic[j]==maxn:\n",
    "                        maxs.append(j)\n",
    "                    elif dic[j]>maxn:\n",
    "                        maxn=dic[j]\n",
    "                        maxs=[j]\n",
    "                \n",
    "            elif i<start:\n",
    "                for j in range(start+1,n+1):\n",
    "                    dic[j]=dic.get(j,0)+1\n",
    "                    if dic[j]==maxn:\n",
    "                        maxs.append(j)\n",
    "                    elif dic[j]>maxn:\n",
    "                        maxn=dic[j]\n",
    "                        maxs=[j]\n",
    "                for j in range(1,i+1):\n",
    "                    dic[j]=dic.get(j,0)+1\n",
    "                    if dic[j]==maxn:\n",
    "                        maxs.append(j)\n",
    "                    elif dic[j]>maxn:\n",
    "                        maxn=dic[j]\n",
    "                        maxs=[j]\n",
    "            start=i\n",
    "        maxs.sort()\n",
    "        return maxs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostVisited(self, n: int, rounds: List[int]) -> List[int]:\n",
    "        start, end = rounds[0], rounds[-1]\n",
    "        if start <= end:\n",
    "            return list(range(start, end + 1))\n",
    "        else:\n",
    "            return list(range(1, end + 1)) + list(range(start, n + 1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostVisited(self, n: int, rounds: List[int]) -> List[int]:\n",
    "        t = [0]*(n+1)\n",
    "        tmp = rounds[0]\n",
    "        for i in rounds[1:]:\n",
    "            if tmp < i:\n",
    "                for j in range(tmp,i):\n",
    "                    t[j] += 1\n",
    "            else:\n",
    "                for j in range(tmp, n+1):\n",
    "                    t[j] += 1\n",
    "                for k in range(1, i):\n",
    "                    t[k] += 1\n",
    "            tmp = i\n",
    "        t[i] += 1\n",
    "        res,p = [],max(t)\n",
    "        for q in range(len(t)):\n",
    "            if t[q] == p:\n",
    "                res.append(q)\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostVisited(self, n: int, rounds: List[int]) -> List[int]:\n",
    "        res=[]\n",
    "        if rounds[0]<=rounds[-1]:\n",
    "            for i in range(rounds[0],rounds[-1]+1):\n",
    "                res.append(i)\n",
    "        else:\n",
    "            for i in range(1,rounds[-1]+1):\n",
    "                res.append(i)\n",
    "            for i in range(rounds[0],n+1):\n",
    "                res.append(i)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# class Solution:\n",
    "#     def mostVisited(self, n: int, rounds: List[int]) -> List[int]:\n",
    "#         arr=[0]*(n+1)\n",
    "#         i=rounds[0]\n",
    "#         arr[i]=1\n",
    "#         m=len(rounds)\n",
    "#         for j in range(m):\n",
    "\n",
    "### 官：模拟+总结           \n",
    "class Solution:\n",
    "    def mostVisited(self,n,rounds):\n",
    "        start,end=rounds[0],rounds[-1]\n",
    "        if start<=end:\n",
    "            return list(range(start,end+1))\n",
    "        else:\n",
    "            return list(range(1,end+1))+list(range(start,n+1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostVisited(self, n: int, rounds: List[int]) -> List[int]:\n",
    "        count_memo = [0]*n\n",
    "        m = len(rounds)\n",
    "        for i in range(1, m):\n",
    "            st, ed = rounds[i-1] - 1, rounds[i] - 1\n",
    "            while st != ed:\n",
    "                count_memo[st % n] += 1\n",
    "                st = (st + 1) % n\n",
    "        count_memo[rounds[-1] - 1] += 1\n",
    "\n",
    "        tmp = sorted([(i+1, x) for i, x in enumerate(count_memo)], key=lambda x:x[1], reverse=True)\n",
    "        # print(tmp)\n",
    "        max_count = tmp[0][1]\n",
    "        ans = [tmp[0][0]]\n",
    "        tmp.pop(0)\n",
    "        while len(tmp) > 0 and tmp[0][1] == max_count:\n",
    "            ans.append(tmp[0][0])\n",
    "            tmp.pop(0)\n",
    "\n",
    "        return ans\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostVisited(self, n: int, rounds: List[int]) -> List[int]:\n",
    "        if rounds[-1]>=rounds[0]:\n",
    "            return [i for i in range(rounds[0],rounds[-1]+1)]\n",
    "        else:\n",
    "            return [i for i in range(1,rounds[-1]+1)]+[j for j in range(rounds[0],n+1)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostVisited(self, n: int, rounds: List[int]) -> List[int]:\n",
    "        start, end = rounds[0], rounds[-1]\n",
    "        if start <= end:\n",
    "            return list(range(start, end + 1))\n",
    "        else:\n",
    "            leftPart = range(1, end + 1)\n",
    "            rightPart = range(start, n + 1)\n",
    "            return list(itertools.chain(leftPart, rightPart))\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostVisited(self, n: int, rounds: List[int]) -> List[int]:\n",
    "        #key-扇区号  value-出现次数\n",
    "        dic = {}\n",
    "        ans = []\n",
    "        m = len(rounds)\n",
    "        for i in range(m-1):\n",
    "            if i==0:\n",
    "                start = rounds[i]\n",
    "            else:\n",
    "                if rounds[i]+1!=n:\n",
    "                    start = (rounds[i]+1)%n\n",
    "                else:\n",
    "                    start = n\n",
    "            end = rounds[i+1]\n",
    "            while start!=end:\n",
    "                dic[start] = dic.get(start,0)+1\n",
    "                if start+1!=n:\n",
    "                    start = (start+1)%n    \n",
    "                else:\n",
    "                    start = n\n",
    "            dic[end] = dic.get(end,0)+1\n",
    "        max_time = max(dic.values())\n",
    "        for key,val in dic.items():\n",
    "            if val == max_time:\n",
    "                ans.append(key)\n",
    "        ans.sort()\n",
    "        return ans\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostVisited(self, n: int, rounds: List[int]) -> List[int]:\n",
    "        matix = [i for i in range(1,n+1)]\n",
    "        res=[]\n",
    "        max_num=[]\n",
    "        length=len(rounds)\n",
    "        if rounds[0] < rounds[length-1]:\n",
    "            res=[x for x in matix[rounds[0]-1:rounds[length-1]]]\n",
    "        elif rounds[0] > rounds[length-1]:\n",
    "            res=[x for x in matix[rounds[0]-1:]]\n",
    "            res=res+matix[0:rounds[length-1]]\n",
    "        else:\n",
    "            res.append(rounds[0])\n",
    "        res.sort()\n",
    "        return res\n",
    "          "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostVisited(self, n: int, rounds: List[int]) -> List[int]:\n",
    "        start = rounds[0]\n",
    "        end = rounds[-1]\n",
    "        if end >= start:\n",
    "            return [*range(start, end + 1)]\n",
    "        return [*chain(range(1, end + 1), range(start, n + 1))]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from collections import Counter\n",
    "\n",
    "class Solution:\n",
    "    def mostVisited(self, n: int, rounds: List[int]) -> List[int]:\n",
    "        def rcount(n,rounds):\n",
    "            r = rounds[0]\n",
    "            ans = [r]\n",
    "            for i in range(1,len(rounds)):\n",
    "                while r != rounds[i]:\n",
    "                    r += 1\n",
    "                    if r > n:\n",
    "                        r = 1\n",
    "                    ans.append(r)\n",
    "            return ans\n",
    "                \n",
    "        ds = Counter(rcount(n,rounds))\n",
    "        ans,save = [],0\n",
    "        for k,v in ds.items():\n",
    "            if v > save:\n",
    "                ans = [k]\n",
    "                save = v\n",
    "            elif v == save:\n",
    "                ans.append(k)\n",
    "        return sorted(ans)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostVisited(self, n: int, rounds: List[int]) -> List[int]:\n",
    "        mm = rounds[-1] - rounds[0]\n",
    "        if mm >= 0:\n",
    "            return [i for i in range(rounds[0], rounds[0]+mm+1)]\n",
    "        if mm < 0:\n",
    "            lst1 = [i for i in range(rounds[0], n+1)]\n",
    "            lst2 = [i for i in range(1, rounds[-1]+1, 1)]\n",
    "            lst2.extend(lst1)\n",
    "            return lst2\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostVisited(self, n: int, rounds: List[int]) -> List[int]:\n",
    "        step = 0\n",
    "        start = rounds[0]\n",
    "        for i in range(1,len(rounds)):\n",
    "            if rounds[i-1] < rounds[i]:\n",
    "                step += (rounds[i]-rounds[i-1])\n",
    "            elif rounds[i-1] > rounds[i]:\n",
    "                step += (rounds[i]+ n -rounds[i-1])\n",
    "\n",
    "        rem = step % n # if rem == 0 return to start\n",
    "        print(rem)\n",
    "        \n",
    "        res = list(range(start,start+rem+1))\n",
    "        for j in range(len(res)):\n",
    "            if res[j] > n:\n",
    "                res[j] -= n\n",
    "        \n",
    "        res.sort()\n",
    "\n",
    "        return res\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostVisited(self, n: int, rounds: List[int]) -> List[int]:\n",
    "        first = rounds[0]\n",
    "        if rounds[-1] == first:\n",
    "            return [first]\n",
    "        elif rounds[-1] > first:\n",
    "            return [i for i in range(first, rounds[-1]+1)]\n",
    "        else:\n",
    "            return [i for i in range(1, rounds[-1]+1)] + [i for i in range(first, n+1)]\n",
    "\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostVisited(self, n: int, rounds: List[int]) -> List[int]:\n",
    "        start = rounds[0]\n",
    "        end = rounds[-1]\n",
    "        if end > start:\n",
    "            return list(range(start, end + 1))\n",
    "        if end == start:\n",
    "            return [start]\n",
    "        return list(chain(range(1, end+1), range(start, n+1))) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostVisited(self, n: int, rounds: List[int]) -> List[int]:\n",
    "        r = [0] * (n + 1)\n",
    "        for i in range(len(rounds) - 1):\n",
    "            x, y = rounds[i], rounds[i + 1]\n",
    "            if x < y:\n",
    "                for z in range(x, y):\n",
    "                    r[z] += 1\n",
    "            else:\n",
    "                for z in range(x, n + 1):\n",
    "                    r[z] += 1\n",
    "                for z in range(1, y):\n",
    "                    r[z] += 1\n",
    "        y = rounds[-1]\n",
    "        r[y] += 1\n",
    "        m = max(r)\n",
    "        l = []\n",
    "        for i in range(1, n + 1):\n",
    "            if r[i] == m:\n",
    "                l.append(i)\n",
    "        return l"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostVisited(self, n: int, rounds: List[int]) -> List[int]:\n",
    "        listans=[]\n",
    "        if rounds[-1]==rounds[0]:\n",
    "            listans.append(rounds[-1])\n",
    "            return listans\n",
    "        elif rounds[-1]>rounds[0]:\n",
    "            for i in range(rounds[0],rounds[-1]+1):\n",
    "                listans.append(i)\n",
    "            return listans\n",
    "        else:\n",
    "            for i in range(rounds[-1]):\n",
    "                listans.append(i+1)\n",
    "            for i in range(rounds[0],n+1):\n",
    "                listans.append(i)\n",
    "            return listans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostVisited(self, n: int, rounds: List[int]) -> List[int]:\n",
    "        lst = []\n",
    "        if rounds[0]<rounds[1]:\n",
    "            lst.extend(range(rounds[0],rounds[1]+1))\n",
    "        else:\n",
    "            lst.extend(range(rounds[0],n+1))\n",
    "            lst.extend(range(1,rounds[1]+1))\n",
    "        for i in range(2,len(rounds)):\n",
    "            if rounds[i-1]<rounds[i]:\n",
    "                lst.extend(range(rounds[i-1]+1,rounds[i]+1))\n",
    "            else:\n",
    "                lst.extend(range(rounds[i-1]+1,n+1))\n",
    "                lst.extend(range(1,rounds[i]+1))\n",
    "        set1 = set(lst)\n",
    "        max1 = max(lst.count(i) for i in set1)\n",
    "        res = []\n",
    "        for i in set1:\n",
    "            if lst.count(i)==max1:\n",
    "                res.append(i)\n",
    "        return sorted(list(set(res)))\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostVisited(self, n: int, rounds: List[int]) -> List[int]:\n",
    "        start = rounds[0]\n",
    "        end = rounds[-1] + 1\n",
    "        if end > start:\n",
    "            return [*range(start, end)]\n",
    "        return [*chain(range(1, end), range(start, n + 1))]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostVisited(self, n: int, rounds: List[int]) -> List[int]:\n",
    "        lst = []\n",
    "        if rounds[0]<rounds[1]:\n",
    "            lst.extend(range(rounds[0],rounds[1]+1))\n",
    "        else:\n",
    "            lst.extend(range(rounds[0],n+1))\n",
    "            lst.extend(range(1,rounds[1]+1))\n",
    "        for i in range(2,len(rounds)):\n",
    "            if rounds[i-1]<rounds[i]:\n",
    "                lst.extend(range(rounds[i-1]+1,rounds[i]+1))\n",
    "            else:\n",
    "                lst.extend(range(rounds[i-1]+1,n+1))\n",
    "                lst.extend(range(1,rounds[i]+1))\n",
    "        max1 = max(lst.count(i) for i in lst)\n",
    "        res = []\n",
    "        for i in lst:\n",
    "            if lst.count(i)==max1 and i not in res:\n",
    "                res.append(i)\n",
    "        return sorted(res)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostVisited(self, n: int, rounds: List[int]) -> List[int]:\n",
    "        start= rounds[0]\n",
    "        end = rounds[-1]\n",
    "        if start <= end:\n",
    "            return list(range(start,end+1))\n",
    "        else:\n",
    "            return list(range(1,end+1))+list(range(start,n+1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostVisited(self, n: int, rounds: List[int]) -> List[int]:\n",
    "        start, end = rounds[0], rounds[-1]\n",
    "        if start <= end:\n",
    "            ans = list(range(start, end + 1))\n",
    "        else:\n",
    "            ans = list(range(1, end + 1)) + list(range(start,n + 1))\n",
    "        return ans\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostVisited(self, n: int, rounds: List[int]) -> List[int]:\n",
    "        # 1-n\n",
    "        # 第i个阶段 从i-1到i\n",
    "        # 扇区从1开始到n结束\n",
    "        # 开闭 最后加1\n",
    "        cnt = 1\n",
    "        for i in range(1,len(rounds)):\n",
    "            if rounds[i]<rounds[i-1]:\n",
    "                cnt += rounds[i]+n-rounds[i-1]\n",
    "            else:\n",
    "                cnt += rounds[i]-rounds[i-1]\n",
    "        cnt %= n\n",
    "        if cnt==0:\n",
    "            cnt=n\n",
    "        ans = []\n",
    "        for i in range(cnt):\n",
    "            t=rounds[0]+i \n",
    "            ans.append(t%n if t>n else t)\n",
    "        ans.sort()\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostVisited(self, n: int, rounds: List[int]) -> List[int]:\n",
    "        ans = []\n",
    "        if rounds[0] <= rounds[-1]:\n",
    "            for i in range(rounds[0], rounds[-1] + 1):\n",
    "                ans.append(i)\n",
    "        else:\n",
    "            for i in range(1, rounds[-1] + 1):\n",
    "                ans.append(i)\n",
    "            for i in range(rounds[0], n + 1):\n",
    "                ans.append(i)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostVisited(self, n: int, rounds: List[int]) -> List[int]:\n",
    "        nums = [0 for i in range(n)]\n",
    "        \n",
    "        for i in range(len(rounds)-1):\n",
    "            if i == 0:\n",
    "                if rounds[i+1] >= rounds[i]:\n",
    "                    for j in range(rounds[i], rounds[i+1]+1):\n",
    "                        nums[j-1] += 1\n",
    "                else:\n",
    "                    for j in range(rounds[i], n+1):\n",
    "                        nums[j-1] += 1\n",
    "                    for k in range(1, rounds[i+1]+1):\n",
    "                        nums[k-1] += 1\n",
    "            else:\n",
    "                if rounds[i+1] >= rounds[i]:\n",
    "                    for j in range(rounds[i]+1, rounds[i+1]+1):\n",
    "                        nums[j-1] += 1\n",
    "                else:\n",
    "                    for j in range(rounds[i]+1, n+1):\n",
    "                        nums[j-1] += 1\n",
    "                    for k in range(1, rounds[i+1]+1):\n",
    "                        nums[k-1] += 1\n",
    "        res = []\n",
    "        max_value = max(nums)\n",
    "        for i in range(len(nums)):\n",
    "            if nums[i] >= max_value:\n",
    "                res.append(i+1)\n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostVisited(self, n: int, rounds: List[int]) -> List[int]:\n",
    "        nums = [0 for i in range(n)]\n",
    "        \n",
    "        for i in range(len(rounds)-1):\n",
    "            if i == 0:\n",
    "                if rounds[i+1] >= rounds[i]:\n",
    "                    for j in range(rounds[i], rounds[i+1]+1):\n",
    "                        nums[j-1] += 1\n",
    "                else:\n",
    "                    for j in range(rounds[i], n+1):\n",
    "                        nums[j-1] += 1\n",
    "                    for k in range(1, rounds[i+1]+1):\n",
    "                        nums[k-1] += 1\n",
    "            else:\n",
    "                if rounds[i+1] >= rounds[i]:\n",
    "                    for j in range(rounds[i]+1, rounds[i+1]+1):\n",
    "                        nums[j-1] += 1\n",
    "                else:\n",
    "                    for j in range(rounds[i]+1, n+1):\n",
    "                        nums[j-1] += 1\n",
    "                    for k in range(1, rounds[i+1]+1):\n",
    "                        nums[k-1] += 1\n",
    "        res = []\n",
    "        max_value = max(nums)\n",
    "        for i in range(len(nums)):\n",
    "            if nums[i] >= max_value:\n",
    "                res.append(i+1)\n",
    "        return res\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "        print(nums)\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostVisited(self, n: int, rounds: List[int]) -> List[int]:\n",
    "        ##脑筋急转弯    rounds反正是跑圈，只需要关心 起点中终点， 跑了几圈经过的所有扇区都+1， 也就等价于都没加\n",
    "        stt = rounds[0]   #初始圈\n",
    "        end = rounds[-1]    #结束圈\n",
    "        if end>= stt:\n",
    "            return [x for x in range(stt, end+1)]\n",
    "        else:\n",
    "            return sorted([x for x in range(stt, n+1)] + [x for x in range(1,end+1)])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostVisited(self, n: int, rounds: List[int]) -> List[int]:\n",
    "\n",
    "        f = [0] * n \n",
    "        m = len(rounds)\n",
    "        f[rounds[0]-1]+=1\n",
    "        for i in range(1, m):\n",
    "            s, e = rounds[i-1]-1, rounds[i] -1\n",
    "            s = (s + 1) % n \n",
    "            f[s] += 1\n",
    "            while s != e:\n",
    "                s = (s+1)%n\n",
    "                f[s] +=1\n",
    "            \n",
    "        mx = max(f)\n",
    "        ans = []\n",
    "        for i in range(n):\n",
    "            if mx == f[i]:\n",
    "                ans.append(i+1)\n",
    "        return ans "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostVisited(self, n: int, rounds: List[int]) -> List[int]:\n",
    "        lst = []\n",
    "        if rounds[0]<rounds[1]:\n",
    "            lst.extend(range(rounds[0],rounds[1]+1))\n",
    "        else:\n",
    "            lst.extend(range(rounds[0],n+1))\n",
    "            lst.extend(range(1,rounds[1]+1))\n",
    "        for i in range(2,len(rounds)):\n",
    "            if rounds[i-1]<rounds[i]:\n",
    "                lst.extend(range(rounds[i-1]+1,rounds[i]+1))\n",
    "            else:\n",
    "                lst.extend(range(rounds[i-1]+1,n+1))\n",
    "                lst.extend(range(1,rounds[i]+1))\n",
    "        max1 = max(lst.count(i) for i in set(lst))\n",
    "        res = []\n",
    "        for i in set(lst):\n",
    "            if lst.count(i)==max1:\n",
    "                res.append(i)\n",
    "        return sorted(list(set(res)))\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostVisited(self, n: int, rounds: List[int]) -> List[int]:\n",
    "        lst = []\n",
    "        if rounds[0]<rounds[1]:\n",
    "            lst.extend(range(rounds[0],rounds[1]+1))\n",
    "        else:\n",
    "            lst.extend(range(rounds[0],n+1))\n",
    "            lst.extend(range(1,rounds[1]+1))\n",
    "        for i in range(2,len(rounds)):\n",
    "            if rounds[i-1]<rounds[i]:\n",
    "                lst.extend(range(rounds[i-1]+1,rounds[i]+1))\n",
    "            else:\n",
    "                lst.extend(range(rounds[i-1]+1,n+1))\n",
    "                lst.extend(range(1,rounds[i]+1))\n",
    "        max1 = max(lst.count(i) for i in lst)\n",
    "        res = []\n",
    "        for i in lst:\n",
    "            if lst.count(i)==max1:\n",
    "                res.append(i)\n",
    "        return sorted(list(set(res)))\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostVisited(self, n: int, rounds: List[int]) -> List[int]:\n",
    "        res = [rounds[0]]\n",
    "        for r in range(len(rounds)-1):\n",
    "            start = rounds[r]\n",
    "            stop = rounds[r+1]\n",
    "            if stop<start: stop+=n\n",
    "            for s in range(start+1,stop+1):\n",
    "                if s>n:\n",
    "                    res.append(s-n)\n",
    "                else:\n",
    "                    res.append(s)\n",
    "            #print(res)\n",
    "        cou = Counter(res)\n",
    "        m = max(cou.values())\n",
    "        res = []\n",
    "        for key in cou.keys():\n",
    "            if cou[key]==m: res.append(key)\n",
    "        return sorted(res)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostVisited(self, n: int, rounds: List[int]) -> List[int]:\n",
    "        start, end = rounds[0], rounds[-1]\n",
    "        if end >= start:\n",
    "            return list(range(start, end + 1))\n",
    "        else:\n",
    "            return list(range(1, end + 1)) + list(range(start, n + 1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostVisited(self, n: int, rounds: List[int]) -> List[int]:\n",
    "        if rounds[0] == rounds[-1]: return [rounds[0]]\n",
    "        rounding = (rounds[-1] + 1) % n\n",
    "        rounding = rounding if rounding != 0 else n\n",
    "        if rounds[0] == rounding: return list(range(1, n+1))\n",
    "        if rounds[0] < rounds[-1]: return list(range(rounds[0], rounds[-1]+1))\n",
    "        result = []\n",
    "        for i in range(1, rounds[-1]+1): result.append(i)\n",
    "        for i in range(rounds[0], n+1): result.append(i)\n",
    "        return result\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostVisited(self, n: int, rounds: List[int]) -> List[int]:\n",
    "        lst = []\n",
    "        if rounds[0]<rounds[1]:\n",
    "            lst.extend(range(rounds[0],rounds[1]+1))\n",
    "        else:\n",
    "            lst.extend(range(rounds[0],n+1))\n",
    "            lst.extend(range(1,rounds[1]+1))\n",
    "        for i in range(2,len(rounds)):\n",
    "            if rounds[i-1]<rounds[i]:\n",
    "                lst.extend(range(rounds[i-1]+1,rounds[i]+1))\n",
    "            else:\n",
    "                lst.extend(range(rounds[i-1]+1,n+1))\n",
    "                lst.extend(range(1,rounds[i]+1))\n",
    "        max1 = max(lst.count(i) for i in set(lst))\n",
    "        res = []\n",
    "        for i in lst:\n",
    "            if lst.count(i)==max1:\n",
    "                res.append(i)\n",
    "        return sorted(list(set(res)))\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from typing import List\n",
    "class Solution:\n",
    "    def mostVisited(self, n: int, rounds: List[int]) -> List[int]:\n",
    "        cnt = [0]*(n+1)\n",
    "        i = 1\n",
    "        s = rounds[0]\n",
    "        cnt[s] = 1\n",
    "        while i < len(rounds):\n",
    "            while s != rounds[i]:\n",
    "                s += 1\n",
    "                if s == n+1:\n",
    "                    s = 1\n",
    "                cnt[s] += 1\n",
    "            i += 1\n",
    "        m = max(cnt)\n",
    "        ans = []\n",
    "        for i in range(1, n+1):\n",
    "            if cnt[i] == m:\n",
    "                ans.append(i)\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostVisited(self, n: int, rounds: List[int]) -> List[int]:\n",
    "        dic = {}\n",
    "        for i in range(1,n+1):\n",
    "            dic[i] = 0\n",
    "        for i in range(1,len(rounds)):\n",
    "            start = rounds[i-1]\n",
    "            end = rounds[i]\n",
    "            if start <= end:\n",
    "                for j in range(start,end):\n",
    "                    dic[j] += 1\n",
    "            else:\n",
    "                for j in range(start,n+1):\n",
    "                    dic[j] += 1\n",
    "                for j in range(1,end):\n",
    "                    dic[j] += 1\n",
    "        dic[rounds[-1]] += 1\n",
    "        print(dic)\n",
    "        res = []\n",
    "        count = 0\n",
    "        for i in dic:\n",
    "            if dic[i] > count:\n",
    "                res = [i]\n",
    "                count = dic[i]\n",
    "            elif dic[i] == count:\n",
    "                res.append(i)\n",
    "        res.sort()\n",
    "        return res"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
